<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <nav id="menu">
      <ul>
        <li>
          <a href="#" data-id="1">菜单1</a>
        </li>
        <li>
          <a href="#" data-id="2">菜单2</a>
        </li>
        <li>
          <a href="#" data-id="3">菜单3</a>
        </li>
      </ul>

      <div>
        <ul style="display: none">
          <li><a href="#">子菜单1-1</a></li>
          <li><a href="#">子菜单1-2</a></li>
        </ul>
        <ul style="display: none">
          <li><a href="#">子菜单2-1</a></li>
          <li><a href="#">子菜单2-2</a></li>
        </ul>
        <ul style="display: none">
          <li><a href="#">子菜单3-1</a></li>
          <li><a href="#">子菜单3-2</a></li>
        </ul>
      </div>
    </nav>

    <script>
      const nav = document.querySelector("#menu");
    //   const arr = document.querySelectorAll("div ul");

      nav.addEventListener("click", function (e) {
        // console.log(e);
        if (e.target.tagName === "A") {
          //   console.log(e.target);
          const rem = e.target.dataset.id;
        //   document.querySelector(`div ul:nth-child(${rem})`).style.display =
        //     "block";
        const menu = document.querySelector(`div ul:nth-child(${rem})`)

        if(menu.style.display === 'none'){
            menu.style.display = 'block'
        }else {
            menu.style.display = 'none'
        }

            // if(document.querySelector(`div ul:nth-child(${rem})`).style.display === 'none'){
            //     document.querySelector(`div ul:nth-child(${rem})`).style.display = 'block'
            // }else {
            //     document.querySelector(`div ul:nth-child(${rem})`).style.display = 'none'
            // }


        }
      });
    </script>
  </body>
</html>
